class ClaimsController < ApplicationController
  include AuthenticatedSystem
  before_filter :check_if_claimed, :except => :edit
  
  def new
    # Render new
  end
  
  def create
    @author.claim!
  end
  
  def edit
    @authentication_code = params[:authentication_code]
    @author = @authentication_code.blank? ? nil : Author.find_by_activation_code(@authentication_code)
    if @author
      #Render Edit Form
    else
      @code_too_short = @authentication_code.length < 40 rescue false
      render :action => 'authentication_code_input', :status => 400
    end
  end
  
  def update
    @author.attributes = params[:author]
    if @author.valid?
      @author.activate!
      self.current_author = @author
      redirect_to author_path(@author)
    else
      render :action => 'edit'
    end
  end
  
  private
    def check_if_claimed
      find_unclaimed_author || already_been_claimed
    end
    
    def find_unclaimed_author
      !find_author.active?
    end
    
    def find_author
      @author ||= Author.find(params[:author_id])
    end
    
    def already_been_claimed
      render :action => 'already_claimed', :status => 409
      false
    end
end
